package com.rmb.controller;

import com.rmb.annotation.SystemLog;
import com.rmb.common.model.response.CommonCode;
import com.rmb.common.model.response.ResponseResult;
import com.rmb.entity.TaskInfo;
import com.rmb.enums.LogType;
import com.rmb.service.TaskConditionService;
import com.rmb.service.TaskInfoService;
import com.rmb.thread.TaskManger;
import com.rmb.thread.ThreadManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;


/**
 * @author wan
 */
@RestController
@RequestMapping("/rmb/taskInfo")
public class TaskInfoController {

    @Autowired
    private TaskInfoService taskInfoService;

    @Autowired
    private TaskConditionService taskConditionService;

    @GetMapping("/findList")
    @SystemLog(description = "查询所有的数据",type = LogType.SELETE)
    public List<TaskInfo> findList(){
        return taskInfoService.findList();
    }

    @PostMapping("/selectTaskInfoList/{pageNum}/{pageSize}")
    @SystemLog(description = "查询模糊",type = LogType.SELETE)
    public List<TaskInfo> selectTaskInfoList(@RequestBody TaskInfo taskInfo,
                                             @PathVariable("pageNum") Integer pageNum,
                                             @PathVariable("pageSize") Integer pageSize) {

        return taskInfoService.selectTaskInfoList(taskInfo, pageNum, pageSize);
    }

    /**
     * 删除指定任务
     * @param taskId 任务ID
     * @return ResponseResult 结果集
     */
    @DeleteMapping("/removeOne/{id}")
    @SystemLog(description = "删除数据",type = LogType.DELETE)
    public ResponseResult removeTaskByTaskId(@PathVariable("id")Long taskId){
        boolean removeStatus = taskInfoService.removeTaskByTaskId(taskId);
        if(!removeStatus){
            return new ResponseResult(CommonCode.FAIL);
        }
        return new ResponseResult(CommonCode.SUCCESS);
    }


    /**
     * 批量任务删除
     * @param taskIds 任务ID
     * @return 结果集
     */
    @DeleteMapping("/removeAll")
    @SystemLog(description = "执行删除任务",type = LogType.DELETE)
    public ResponseResult removeTaskBySelect(@RequestParam("taskId") List<Long> taskIds){
        taskIds.forEach(System.out::println);
        StringBuffer removeTaskBySelect = taskInfoService.removeTaskBySelect(taskIds);
        if(removeTaskBySelect==null){
            new ResponseResult(CommonCode.FAIL);
        }
        return new ResponseResult(CommonCode.SUCCESS);
    }

    /**
     * 添加任务功能
     * @param taskInfo 实体类
     * @return 添加结果
     */
    @PostMapping("/saveTask")
    @SystemLog(description = "添加任务功能",type = LogType.INSERT)
    public TaskInfo saveTask(@RequestBody TaskInfo taskInfo) {
        return taskInfoService.saveTaskInfo(taskInfo);
    }

    /**
     * 吕东 2021/01/10
     * @param file 文件
     * @param request request
     * @param response response
     * @return 转换结果
     */
    @RequestMapping(value="/upload",method = RequestMethod.POST)
    @ResponseBody
    @SystemLog(description = "添加任务功能",type = LogType.INSERT)
    public String  upload(@RequestParam(value="file",required = false) MultipartFile file, HttpServletRequest request, HttpServletResponse response) {
        return taskInfoService.readExcelFile(file);
    }

    @GetMapping("/task")
    @SystemLog(description = "查询功能",type = LogType.SELETE)
    public ResponseResult taskId() {
        List<TaskInfo> list = taskInfoService.findList();
        for (TaskInfo info : list) {

            ThreadManager.getThreadPoolProxy().execute(new TaskManger());
        }
        return new ResponseResult(CommonCode.SUCCESS);
    }
}
